<?php
include("includes/config.inc.php");
include("includes/functions.inc.php");
include("includes/classes/Text.php");

$text = new Text;
$text->db = $db;

if (empty($_GET['action'])) {
    include("includes/classes/Image.php");
    $image = new Image;
    $smarty->assign("stockImages", $image->getStockImages());
    $text->viewPart = $_GET['viewPart'];
    $smarty->assign("viewPart", $_GET['viewPart']);
    $smarty->assign("text", $text->getText(false));
    $smarty->display("general/update.tpl");
} elseif ($_GET['action'] == "getStockImages") {
    include("includes/classes/Image.php");
    $image = new Image;
    $smarty->assign("stockImages", $image->getStockImages());
    $smarty->display("general/stockimages.tpl");
} elseif ($_GET['action'] == "update") {
    $text->updateText($_GET['viewPart'], $_POST['textfield']);
} elseif ($_GET['action'] == "get_text_after_update") {
    $text->viewPart = $_GET['viewPart'];
    print $text->getText(true);
} elseif ($_GET['action'] == "item_picture") {
    include("includes/classes/Upload.php");
    $upload = new Upload;
    $upload->rootId = $_POST['item_id'];
    $upload->file = $_FILES['item_picture'];
    $upload->filename_part = $_GET['site'];
    $upload->path = HOME_PATH . "images/menu_pics/";
    $upload->filename = $upload->uploadFile();
    $upload->return = $upload->makePicture(191, 191);
    if ($upload->return == "size")
        print "Das hochzuladende Bild muss quadratisch und gr&ouml;&szlig; oder gleich 191px sein.";
    elseif ($upload->return == "type")
        print "Die hochzuladende Datei darf nur vom Typ JPG oder PNG sein.";
    else {
        include("includes/classes/Menu.php");
        $menu = new Menu;
        $menu->db = $db;
        $rootItem = $menu->getMenuItemById($upload->rootId);
        @unlink(HOME_PATH . "images/" . $rootItem['Picture']);        
        $upload->db = $db;
        $upload->storeDBData();
        chmod(HOME_PATH . "images/menu_pics/" . $upload->return, 0777);
        print "Das Bild wurde erfolgreich hochgeladen.";
    }
} elseif ($_GET['action'] == "content_image") {
    include("includes/classes/Upload.php");
    $upload = new Upload;
    $upload->file = $_FILES['content_image'];
    $upload->path = HOME_PATH . "images/content/";
    $upload->filename = $upload->uploadFile();
    if ($upload->filename != 999) {
        chmod(HOME_PATH . "images/content/" . $upload->filename, 0777);
        print "Das Bild wurde erfolgreich hochgeladen.";
    } else
        print "Es gab einen Fehler beim Hochladen des Bildes.\nM%F6glicherweise ist das Bild zu gro%DF!";
} elseif ($_GET['action'] == "deleteContentImage") {
    if (@unlink(HOME_PATH . "images/content/" . $_GET['image']))
        print "Bild wurde erfolgreich gel%F6scht!";
    else
        print "Es gab einen Fehler beim L%F6schen des Bildes!";
}
?>